﻿using System.Windows;
using System.Windows.Controls;
using AvalonDock;
using System;
using System.Reflection;

namespace RiskMan.Config
{
    /// <summary>
    /// Логика взаимодействия для UserControl1.xaml
    /// </summary>
    public partial class MainSettings : DocumentContent
    {
        private MainWindow mainWindow;

        public MainSettings()
        {
            try
            {
                this.mainWindow = RiskMan.MainWindow.StaticLevel;
                InitializeComponent();

                StartSessionHH.Text = RiskMan.Properties.Settings.Default.StartSessionHH.ToString();
                StartSessionMM.Text = RiskMan.Properties.Settings.Default.StartSessionMM.ToString();
                StopSessionFortsHH.Text = RiskMan.Properties.Settings.Default.StopSessionFortsHH.ToString();
                StopSessionFortsMM.Text = RiskMan.Properties.Settings.Default.StopSessionFortsMM.ToString();


                TextBox_HHdk.Text = Properties.Settings.Default.StartDay.Hour.ToString();
                TextBox_MMdk.Text = Properties.Settings.Default.StartDay.Minute.ToString();

                TextBox_HHnk.Text = Properties.Settings.Default.StartNight.Hour.ToString();
                TextBox_MMnk.Text = Properties.Settings.Default.StartNight.Minute.ToString();


                TimeSpan ts = Properties.Settings.Default.EndDay.Subtract(Properties.Settings.Default.StartDay);
                TextBox_Idk.Text = ts.TotalMinutes + "";

                ts = Properties.Settings.Default.EndNight.Subtract(Properties.Settings.Default.StartNight);
                TextBox_Ink.Text = ts.TotalMinutes + "";

            }
            catch (Exception ex)
            {
                Cfg.SetLog(ex.Message, MainWindow.StaticLevel.ListBox_Logs, GetType(), MethodBase.GetCurrentMethod());
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.StartDay = new DateTime(DateTime.Now.Year,
                                                                DateTime.Now.Month,
                                                                DateTime.Now.Day,
                                                                Convert.ToInt32(TextBox_HHdk.Text),
                                                                Convert.ToInt32(TextBox_MMdk.Text), 0);
            DateTime endday = Properties.Settings.Default.StartDay;
            double inter = Convert.ToDouble(TextBox_Idk.Text);
            DateTime temp = endday.AddMinutes(inter);

            Properties.Settings.Default.EndDay = temp;

            Properties.Settings.Default.StartNight = new DateTime(DateTime.Now.Year,
                                                                DateTime.Now.Month,
                                                                DateTime.Now.Day,
                                                                Convert.ToInt32(TextBox_HHnk.Text),
                                                                Convert.ToInt32(TextBox_MMnk.Text), 0);
            DateTime endnight = new DateTime(DateTime.Now.Year,
                                           DateTime.Now.Month,
                                           DateTime.Now.Day,
                                           Convert.ToInt32(TextBox_HHnk.Text),
                                           Convert.ToInt32(TextBox_MMnk.Text), 0);
            inter = Convert.ToDouble(TextBox_Ink.Text);
            temp = endnight.AddMinutes(inter);
            Properties.Settings.Default.EndNight = temp;

            Properties.Settings.Default.Save();

            mainWindow.startNight = Properties.Settings.Default.StartNight;
            mainWindow.endNight = Properties.Settings.Default.EndNight;
        }



        private void StartSessionHH_TextChanged(object sender, TextChangedEventArgs e)
        {
            RiskMan.Properties.Settings.Default.StartSessionHH = (int)Cfg.ToDoubleFormControls(StartSessionHH.Text);
            RiskMan.Properties.Settings.Default.Save();
        }

        private void StartSessionMM_TextChanged(object sender, TextChangedEventArgs e)
        {
            RiskMan.Properties.Settings.Default.StartSessionMM = (int)Cfg.ToDoubleFormControls(StartSessionMM.Text);
            RiskMan.Properties.Settings.Default.Save();
        }

     private void StopSessionFortsMM_TextChanged(object sender, TextChangedEventArgs e)
        {
            RiskMan.Properties.Settings.Default.StopSessionFortsMM = (int)Cfg.ToDoubleFormControls(StopSessionFortsMM.Text);
            RiskMan.Properties.Settings.Default.Save();
        }

        private void StopSessionFortsHH_TextChanged(object sender, TextChangedEventArgs e)
        {
            RiskMan.Properties.Settings.Default.StopSessionFortsHH = (int)Cfg.ToDoubleFormControls(StopSessionFortsHH.Text);
            RiskMan.Properties.Settings.Default.Save();
        }


        private void txbxTransactionInTheSecond_PreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
        {
            e.Handled = "0123456789".IndexOf(e.Text) < 0;
        }


    }
}